package com.avast.personalbudgetapp.presenter.custom;

import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.Locale;

import android.app.Activity;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.TextView;

import com.avast.personalbudgetapp.R;
import com.avast.personalbudgetapp.global.constant.Constants;
import com.avast.personalbudgetapp.model.bean.Proto.Transaction;

/**
 * Custom adapter to display each transaction as a list item with date, kind and value fields
 * 
 * @author hallac
 *
 */
public class CustomListViewAdapter extends ArrayAdapter<Transaction> {

	Context context;
	
	public CustomListViewAdapter(Context context, 
			int resource, List<Transaction> objects) {
		super(context, resource, objects);
		this.context = context;
	}
    
    public View getView(int position, View convertView, ViewGroup parent) {
    	
    	DateFormat df = new SimpleDateFormat(Constants.APP_DATE_FORMAT, Locale.getDefault());
        LayoutInflater mInflater = (LayoutInflater) context.getSystemService(Activity.LAYOUT_INFLATER_SERVICE);
        CustomViewHolder holder = null;
        Transaction transaction = getItem(position);
		
        if (convertView == null) {
            convertView = mInflater.inflate(R.layout.list_item, null);
            holder = new CustomViewHolder();
            holder.txtDate = (TextView) convertView.findViewById(R.id.textViewDate);
            holder.txtKind = (TextView) convertView.findViewById(R.id.textViewKind);
            holder.txtValue = (TextView) convertView.findViewById(R.id.textViewValue);
            convertView.setTag(holder);
        } else {
			holder = (CustomViewHolder) convertView.getTag();
        }
        
        holder.guid = transaction.getGuid();
		holder.txtDate.setText(df.format(new Date(transaction.getDate())));
		holder.txtKind.setText(transaction.getKind());
		holder.txtValue.setText(String.valueOf(transaction.getValue()));
        
        return convertView;
    }

}
